package com.bw.services.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.lib_base.base.BaseActivity
import com.bw.services.R
import com.bw.services.adapter.NewsAdapter
import com.bw.services.databinding.ActivityNewsBinding
import com.bw.services.entity.NewsEntity
import com.bw.services.viewmodel.ServicesViewModel
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.therouter.TheRouter
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "app/NewsActivity")
class NewsActivity : BaseActivity<ActivityNewsBinding, ServicesViewModel>() {
    
    private lateinit var newsAdapter: NewsAdapter
    private var newsList= mutableListOf<NewsEntity>()
    override fun generatePremission(pageName: String): Array<String> {
        return arrayOf()
    }

    override fun setupUI() {
        // 设置返回按钮点击事件
        binding.ivBack.setOnClickListener {
            finish()
        }
        // 设置添加按钮点击事件
        binding.btnAdd.setOnClickListener {
            TheRouter.build("app/AddNewsActivity").navigation()
        }
        
        // 设置搜索框回车事件
        binding.etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val searchText = binding.etSearch.text.toString().trim()
                if (searchText.isNotEmpty()) {
                    performSearch(searchText)
                }
                return@setOnEditorActionListener true
            }
            false
        }
        
        // 设置RecyclerView
        setupRecyclerView()
    }

    override fun fetchData() {
        // 模拟获取新闻数据
        vm.getNews()
    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.newsData.collect {
                newsList.clear()
                if (it != null) {
                    newsList.addAll(it)
                }
                newsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun handleEvent() {
        // 事件处理已在setupUI中实现
    }

    override val mLayoutResId: Int
        get() = R.layout.activity_news
        
    /**
     * 设置RecyclerView
     */
    private fun setupRecyclerView() {
        newsAdapter = NewsAdapter(newsList)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(this@NewsActivity)
            adapter = newsAdapter
        }
        
        // 设置点击事件
        newsAdapter.onItemClickListener = { position, news ->
            Toast.makeText(this, "点击了: ${news}", Toast.LENGTH_SHORT).show()
            // TODO: 跳转到新闻详情页面
        }
        
        newsAdapter.onViewDetailsClickListener = { position, news ->
            Toast.makeText(this, "查看详情: ${news.title}", Toast.LENGTH_SHORT).show()
            // TODO: 跳转到新闻详情页面
        }
    }

    private fun performSearch(keyword: String) {
        Toast.makeText(this, "搜索: $keyword", Toast.LENGTH_SHORT).show()

    }
}